package cn.zyy.hotel.service.impl;

import cn.zyy.hotel.dao.MenuMapper;
import cn.zyy.hotel.entity.Menu;
import cn.zyy.hotel.service.MenuService;
import cn.zyy.hotel.vo.MenuVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuMapper menuMapper;

    //查询所有菜单列表
    public List<Menu> selectAllMenu() {
        return menuMapper.selectAllMenu();
    }

    //根据角色ID查询该角色拥有的菜单ID集合
    public List<Integer> selectMenuIdListByRoleId(int roleId) {
        return menuMapper.selectMenuIdListByRoleId(roleId);
    }

    //根据菜单编号查询菜单信息
    public List<Menu> selectMenuByMenuId(List<Integer> menuIds) {
        return menuMapper.selectMenuByMenuId(menuIds);
    }

    //查询菜单列表
    public List<Menu> selectMenuListByPage(MenuVO menuVo) {
        return menuMapper.selectMenuListByPage(menuVo);
    }

    //添加菜单
    public int addMenu(Menu menu) {
        //如果没有选择父级菜单，默认设置父级菜单为0
        if (menu.getPid() == null){
            menu.setPid(0);
        }
        //设置打开方式，没有作用
        menu.setTarget("_self");
        return menuMapper.addMenu(menu);
    }

    //修改菜单
    public int updateMenu(Menu menu) {
        return menuMapper.updateMenu(menu);
    }

    //根据菜单Id查询该菜单是否有子菜单
    public int getMenuCountByMenuId(Integer id) {
        return menuMapper.checkMenuHasChild(id);
    }

    //删除菜单
    public int deleteById(Integer id) {
        return menuMapper.deleteById(id);
    }

    //根据当前登录的员工角色查询菜单列表
    public List<Menu> selectMenuListByEmployeeId(Integer employeeId) {
        return menuMapper.selectMenuListByEmployeeId(employeeId);
    }

    //查询菜单是否有用户使用
    @Override
    public int getRoleCountByMenuId(Integer id) {
        return menuMapper.checkRoleHasMenu(id);
    }

}
